package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.*;
import com.atguigu.tingshu.vo.order.TrackPayVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@PostMapping("/uploadTrack")
	public Result<UploadTrackVo> uploadTrack(MultipartFile file){
		UploadTrackVo uploadTrackVo = trackInfoService.uploadTrack(file);
		return Result.ok(uploadTrackVo);
	}
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}

	//api/album/trackInfo
	@PostMapping("/findUserTrackPage/{page}/{limit}")
	public Result<IPage<TrackListVo>> findUserTrackPage(@PathVariable(value = "page") Long pageNo,
														@PathVariable(value = "limit") Long pageSize,
														@RequestBody TrackInfoQuery trackInfoQuery) {
		IPage<TrackListVo> pageModel = trackInfoService.findUserTrackPage(pageNo, pageSize, trackInfoQuery);
		return Result.ok(pageModel);
	}
	///api/album/trackInfo/getTrackInfo/{id}
	@GetMapping("/getTrackInfo/{id}")
	public Result getTrackInfoById(@PathVariable Long id) {
		TrackInfo trackInfo = trackInfoService.getTrackInfoById(id);
		return Result.ok(trackInfo);
	}
	///api/album/trackInfo
	@PutMapping("/updateTrackInfo/{id}")
	public Result updateTrackInfoById(@PathVariable(value = "id") Long trackId, @RequestBody TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfoById(trackId,trackInfoVo);
		return Result.ok();
	}

	@DeleteMapping("/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable(value = "id") Long trackId){
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	@GuiGuLogin(required = false)
	@GetMapping("/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable(value = "albumId") Long albumId,@PathVariable(value = "page") Integer pageNum,@PathVariable(value = "limit") Integer pageSize){
		IPage<AlbumTrackListVo> albumTrackListVoIPage = trackInfoService.findAlbumTrackPage(albumId,pageNum,pageSize);
		return Result.ok(albumTrackListVoIPage);
	}

    @GetMapping("/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable(value = "trackId")Long trackId){
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }

	/**
	 * 根据用户所选择的声音的id查询要购买的声音的分集列表
	 */
	@GuiGuLogin
	@GetMapping(value = "/findUserTrackPaidList/{trackId}")
	public Result<List<TrackPayVo>> findUserTrackPaidList(@PathVariable(value = "trackId") Long trackId) {
		List<TrackPayVo> trackPayVoList = trackInfoService.findUserTrackPaidList(trackId) ;
		return Result.build(trackPayVoList , ResultCodeEnum.SUCCESS) ;
	}

	@GuiGuLogin
	// 根据当前用户所点击的声音的id，以及购买的声音的数量，查询下单声音列表数据
	@GetMapping(value = "/findPaidTrackInfoList/{trackId}/{number}")
	public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable(value = "trackId") Long trackId , @PathVariable(value = "number") Integer num) {
		List<TrackInfo> trackInfoList = trackInfoService.findPaidTrackInfoList(trackId , num) ;
		return Result.build(trackInfoList , ResultCodeEnum.SUCCESS) ;
	}
}

